<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" size="450px" title="查看详情">
    <div style="max-height: 500px; overflow: scroll">
      <el-form
        ref="ruleFormRef"
        label-width="100px"
        label-suffix=" :"
        :rules="rules"
        :disabled="dialogProps.isView"
        :model="dialogProps.row"
        :hide-required-asterisk="dialogProps.isView"
      >
        <el-form-item label="序列" prop="id">
          <el-input v-model="dialogProps.row!.id" clearable />
        </el-form-item>
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="dialogProps.row!.name" />
        </el-form-item>
        <el-form-item label="完成时间" prop="createTime">
          <el-input v-model="dialogProps.row!.createTime" />
        </el-form-item>
        <el-form-item label="是否完成" prop="isSuccess">
          <el-input v-model="dialogProps.row!.isSuccess" />
        </el-form-item>
        <el-form-item label="其他数据" prop="parameters">
          <el-input autosize type="textarea" v-model="dialogProps.row!.parameters" />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button @click="dialogVisible = false"> 取消 </el-button>
      <el-button v-show="!dialogProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="tsx" name="RoleDialog">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Operation } from "@/api/interface";
import { getDepartMentList } from "@/api/modules/operation";

const rules = reactive({
  logDate: [{ required: true, message: "请输入日期" }]
});

interface DialogProps {
  title: string;
  isView: boolean;
  row: Partial<Operation.ResDepartmentList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const dialogVisible = ref(false);
const dialogProps = ref<DialogProps>({
  isView: false,
  title: "",
  row: {}
});

const department = ref([]);

// 接收父组件传过来的参数
const acceptParams = (params: DialogProps) => {
  dialogProps.value = params;
  dialogVisible.value = true;
  getDepartMentList({ pageNum: 1, pageSize: 9999 }).then(res => {
    department.value = res.data.list;
  });
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await dialogProps.value.api!(dialogProps.value.row);
      ElMessage.success({ message: `${dialogProps.value.nickName}角色成功！` });
      dialogProps.value.getTableList!();
      dialogVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
